package dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import util.RunMode;
import entity.BoardMemento;

/**
 * This file stores the status of the board. 
 */
public class BoardMementoDAO {
	public static final String FILE_NAME = "cyberpoetry-slam.sav";
	
	public void saveBoardMemento(BoardMemento bm, RunMode m, String fileName){
		if(m == RunMode.DEFAULT){
			try {
				FileOutputStream fout = new FileOutputStream(fileName);
				ObjectOutputStream oos = new ObjectOutputStream(fout);
				
				oos.writeObject(bm);
				
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Get the status of the board. 
	 */
	public BoardMemento findBoardMementoByFileName(String fileName){
		BoardMemento bm = null;
		
		try {
			FileInputStream fin = new FileInputStream(fileName);
			ObjectInputStream ois = new ObjectInputStream(fin);
			
			bm = (BoardMemento) ois.readObject();
			bm.replaceDuplicates();
			
			ois.close();
			
			System.out.println("Save file successfully loaded.");
		} catch (FileNotFoundException e) {
			System.out.println("The save file wasn't found.");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		return bm;
	}
}
